//app.js
App({
  onLaunch: function () {
    const that = this;
    wx.showLoading({
      title: '初始化中',
      mask: true,
    })
    that.globalData.register = false,
    that.globalData.buyNum = 0,
    that.globalData.typeId = 0,
    that.globalData.ad_id = '',
      wx.getSystemInfo({
        success: function (res) {
          that.globalData.system = {
            barH: res.statusBarHeight,
            ratio: res.screenWidth / 750,
            version: res.version,
          }
        },
      })

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: that.globalData.url + 'SmallScavenging',
          data: {
            code: res.code
          },
          method: 'POST',
          success: function(res) {
            if (res.data.info) {
              that.globalData.openid = res.data.info.openid;
              that.globalData.unionid = res.data.info.unionid;
              that.globalData.session_key = res.data.info.session_key;
              let wx_name = '登录';
              let wx_img = '/image/wx-img.png';
              wx.getUserInfo({
                success: function(res) {
                  that.globalData.userInfo = res.userInfo;
                  wx_name = res.userInfo.nickName;
                  wx_img = res.userInfo.avatarUrl;
                  if (that.userInfoReadyCallback) {
                    that.userInfoReadyCallback(res)
                  }
                },
                complete(res) {
                  wx.request({
                    url: that.globalData.url + 'Obtain',
                    data: {
                      unionid: that.globalData.unionid,
                      openid: that.globalData.openid,
                      wx_name: '点击登录',
                      wx_img: wx_img,
                    },
                    method: 'POST',
                    success: function(res) {
                      if (res.data.info) {
                        // that.globalData.uid = 1;
                        that.globalData.uid = res.data.info.uid;
                        that.globalData.wx_name = res.data.info.wx_name;
                        if (that.idCB) {
                          that.idCB();
                        }
                      }
                      wx.hideLoading();
                    },
                  })
                }
              })
            }
          },
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    url: "https://xlkjb.ten.starfunokb.cn/index/",
  }
})
